package com.kuang.common;

import com.kuang.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 全局异常处理类
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(NullPointerException.class)
    public Result exceptionHandler(NullPointerException e){
        log.info(e.getMessage());
        return Result.error("用户名不存在");
    }

    @ExceptionHandler(ServiceException.class)
    public Result exceptionHandler(ServiceException e){
        log.info(e.getMessage());
        return Result.error(e.getCode(),e.getMessage());
    }


}
